package com.katze.boot.common.utils;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.nio.file.Paths;
import java.util.Objects;
import java.util.Optional;
import java.util.StringJoiner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.IntStream;

/**
 * 工具类
 */
public class CommonUtils {
    public static final String SEPARATOR = "/";

    /**
     * 字符串拼接
     * @param delimiter 拼接符号
     * @param elements 参数内容
     * @return 字符串
     */
    public static String join(CharSequence delimiter, CharSequence... elements) {
        Objects.requireNonNull(delimiter);
        Objects.requireNonNull(elements);
        // Number of elements not likely worth Arrays.stream overhead.
        StringJoiner joiner = new StringJoiner(delimiter);
        for (CharSequence cs: elements) {
            Optional.ofNullable(cs).filter(item -> item.length() > 0).ifPresent(joiner::add);
        }
        return joiner.toString();
    }

    /**
     * 驼峰转下划线
     * @param value 待转换值
     * @param postion 开始下标 默认值：0
     * @return 字符串
     */
    public static String camelToUnderscore(String value, Integer... postion) {
        if (StringUtils.isBlank(value))
            return value;
        String[] arr = StringUtils.splitByCharacterTypeCamelCase(value);
        if (arr.length == 0)
            return value;
        StringBuilder result = new StringBuilder();
        int index  = postion.length > 0? postion[0] : 0;
        IntStream.range(index, arr.length).forEach(i -> {
            if (i != arr.length - 1)
                result.append(arr[i]).append(StringPool.UNDERSCORE);
            else
                result.append(arr[i]);
        });
        return StringUtils.lowerCase(result.toString());
    }

    /**
     * 读取图片
     * @param fullPath 图片全路径
     * @param response 响应
     */
    public static void readImage(String fullPath, HttpServletResponse response){
        File file = Paths.get(fullPath).toFile();
        if(file.exists()){
            try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
                 ByteArrayOutputStream bos = new ByteArrayOutputStream(1024 * 1024 *5)
            ) {
                byte[] cache = new byte[1024 * 1024 *5];
                int length =0;
                while ((length = bis.read(cache)) != -1) {
                    bos.write(cache, 0, length);
                }
                response.getOutputStream().write(bos.toByteArray());
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }

    /**
     * 字符占位符输出
     * @param message 含有{}占位符字符串
     * @param ages 参数
     */
    public static String stringFormat(String message, Object... ages){
        Pattern pt = Pattern.compile("\\{(.*?)}");
        Matcher mt = pt.matcher(message);
        for (Object par : ages) {
            message = mt.replaceFirst(String.valueOf(par));
            mt = pt.matcher(message);
        }
        return message;
    }
}
